//
//  SelectMenuViewController.m
//  Spa
//
//  Created by PangPond on 14/1/2556.
//  Copyright (c) พ.ศ. 2556 innova. All rights reserved.
//

#import "SelectMenuViewController.h"
#import "Menu.h"
#import "DetailCheckInViewController.h"
@interface SelectMenuViewController ()

@end

@implementation SelectMenuViewController{
    NSArray *games;
	NSUInteger selectedIndex;
    NSArray *tempArray;
}

@synthesize databaseUtility =_databaseUtility;
@synthesize menu = _menu;
@synthesize tableView = _tableView;
@synthesize keyDelete;
@synthesize marks,check,priceMenu,priceCount;
@synthesize game;
@synthesize selectMenu;
@synthesize keySelectMenu;
@synthesize selectMenuCount;
@synthesize pickedTags;

@synthesize fetchedResultsController = _fetchedResultsController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self loadView];
    [self viewDidLoad];
}

- (BOOL) hidesBottomBarWhenPushed {
    return YES;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.hidesBottomBarWhenPushed = YES;
    self.databaseUtility = [[DatabaseUtility alloc]initWithDatabaseName:@"Database"];
    self.menu = [self.databaseUtility fetchedResultsWithClass:@"Menu"withSortDescriptorString:@"name"];
    [self.tableView reloadData];
    check = 0;
    priceMenu =0;
    priceCount = 0;
    NSLog(@"Load %d",selectMenu);
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.menu count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //////
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    //config xell

    Menu * Menus = [self.menu objectAtIndex:indexPath.row];
    cell.textLabel.text =[NSString stringWithFormat:@"%@",Menus.name];
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    if ([pickedTags containsObject:cell]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    
    /*
    if (indexPath.row == selectMenu){
        if (keySelectMenu == @"1") {
         cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
	}else{
		cell.accessoryType = UITableViewCellAccessoryNone;
    }
     */
    return cell;
    
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
   // Menu * menu = (Menu *)[self.menu objectAtIndexPath:indexPath];
    NSLog(@"self.fetchedResultsController %@",self.fetchedResultsController);
    Menu * menus = [self.menu objectAtIndex:indexPath.row];

     [thisCell setSelected:NO animated:YES];
    NSMutableArray *muArrayCount  = [[NSMutableArray alloc]init];
    int *intCount;
    
    if (thisCell.accessoryType == UITableViewCellAccessoryNone) {
        thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
        [pickedTags addObject:thisCell];
        
        NSLog(@"1 ");
        
    }else{
        thisCell.accessoryType = UITableViewCellAccessoryNone;
        [pickedTags removeObject:thisCell];
        NSLog(@"2");
    }
    
    tempArray = [[NSArray alloc]initWithArray:muArrayCount];

     //  selectMenuCount = check;
 
}


//funtion

-(IBAction)Done:(id)sender{
   // [self.delegate keySelectMenu:tempArray];
    [self.delegate pickedTags:pickedTags];
    [self.delegate selectMenu:(int *)selectMenu];
    [self.delegate changeTextsMenu:(int *)selectMenuCount];
    [self.delegate priceMenu:(int *)priceCount];
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
